exports.uploadImageFile = function(req,res){
    /**
     * 初始化上传配置
     **/
    var formidable   = require('formidable');
    var U   	     = new formidable.IncomingForm();
    U.encoding       = 'utf-8';
    U.uploadDir      = "./" + config.fileManager.baseDir + "/temp/";
    U.keepExtensions = config.fileManager.keepExtensions;
    U.maxFileSize	 = 200 * 1024 * 1024;

    var fs          = require('fs');
    var path        = require('path');
    
    var oldpath     = "";
    var newFileName = "";
    var dir         = "";
    var newDir      = "";
    var newpath     = "";
    var resultData  = {};
    var isThumbnail   = config.uploadImageConfig.isThumbnail;
    var isWatermark   = config.uploadImageConfig.isWatermark;
    var watermarkType = config.uploadImageConfig.watermarkType;


    //图片验证操作
    function imageValidation(cb){
        U.parse(req, function (error, fields, files) {

            //上传系统错误
            if(error) {

                return RES.errorResponse(res,"图片上传失败，请重新尝试");
            }

            //判断是否支持该文件类型的上传
            var extFileName = files.files.name.split(".")[1];
            if(config.uploadImageConfig.uploadType.indexOf(extFileName) == - 1){

                return RES.errorResponse(res,"图片上传失败，原因：不支持该图片类型的上传，仅支持：" + config.uploadImageConfig.uploadType.join(",") + " 类型的图片");
            }

            //判断文件大小是否允许
            var maxSize = files.files.size;
            if(maxSize > config.fileManager.maxFileSize){

                return RES.errorResponse(res,"图片上传失败，原因：图片大小超出了上传限制，仅允许上传最多 " +  (config.fileManager.maxFileSize/1024/1024) + " M的图片");
            }

            //获取原始名和新名称，进行替换
            oldpath = path.normalize(files.files.path);
            //如果用户设置了要上传文件的名称，则使用，否则使用文件自己默认的
            if(fields.filename != undefined){
                newFileName = fields.filename;
            }else{
                newFileName = files.files.name;
            }
            //判断是否有dir参数，如果有创建对应的文件夹目录
            if(fields.dir != undefined){
                dir = fields.dir + "/";

                newDir = './' + config.fileManager.baseDir + "/" + config.uploadImageConfig.uploadDir + "/" + fields.dir + "/";

                common.mkdirs(newDir,function(){
                    newpath = newDir + newFileName;

                    cb();
    
                })

            }else{
                newpath = './' + config.fileManager.baseDir + "/" + config.uploadImageConfig.uploadDir + "/" + newFileName;
                cb();
            }


        })

    }

    //图片上传操作
    function imageUpload(cb){
        //名称替换操作
        fs.rename(oldpath,newpath,function(err){
            resultData.url = config.server.ip + ":" + config.server.port + "/"+config.uploadImageConfig.uploadDir + "/" + dir + newFileName;

            cb();

        });

    }

    //水印操作
    function watermarkAction(cb){

        if(!isWatermark){
            return cb();
        }

        //判断水印的类型
        if(watermarkType == 'IMAGE'){

            //水印地址
            var wPath = './' + config.fileManager.baseDir + "/" + config.fileManager.configFile + "/" + config.uploadImageConfig.watermarkDefaultImage;

            //打水印-图片
            GM(newpath)
            .composite(wPath)
            .gravity('SouthEast')
            .geometry('+5+5')
            .write(newpath, function (err) {

                if(err){
                    return RES.errorResponse(res,"图片上传失败，请重新尝试，原因：图片水印设置失败");
                }

                cb();

            });
           
        //文字水印    
        }else{

            //打水印-图片
            GM(newpath)
            .fill('#ffffff')
            .font('./fonts/fangzhengkaiti.ttf')
            .fontSize(20)
            .drawText(5,5,config.uploadImageConfig.watermarkDefaultText,'SouthEast')
            .write(newpath, function (err) {

                if(err){
                    return RES.errorResponse(res,"图片上传失败，请重新尝试，原因：文字水印设置失败");
                }

                cb();

            });

        }

    }

    //缩略图操作
    function thumbnailAction(cb){

        if(!isThumbnail){
            return cb();
        }

        //缩略图保存地址
        var thFileName = "th_" + newFileName;
        var thPath = './' + config.fileManager.baseDir + "/" + config.uploadImageConfig.thumbnailDir + "/" + thFileName;


        GM(newpath)
        .resize(config.uploadImageConfig.thumbnailSize).write(thPath, function (err) {
            if (err){
                return RES.errorResponse(res,"图片上传失败，请重新尝试，原因：缩略图生成失败");
            }

            //返回上传后的文件地址
            resultData.th_url = config.server.ip + ":" + config.server.port + "/"+config.uploadImageConfig.thumbnailDir+"/" + thFileName;
       
            cb();
                        
        })

    }


    F.Serial([
        imageValidation,
        imageUpload,
        thumbnailAction,
        watermarkAction
    ],function(){
        return RES.successResponse(res,resultData)
    });

}